<template>
    <el-dialog :close-on-click-modal="false" @open="handleOpen" :modelValue="dialogVisible" @update:modelValue="(e)=>{emit('update:dialogVisible',e)}"   title="Shipping address">
        <el-form :model="formParams">
            <el-form-item label="userName" :label-width="formLabelWidth">
                <el-input v-model="formParams.userName" autocomplete="off" />
            </el-form-item>
            <el-form-item label="name" :label-width="formLabelWidth">
                <el-input v-model="formParams.name" autocomplete="off" />
            </el-form-item>
            <el-form-item label="surname" :label-width="formLabelWidth">
                <el-input v-model="formParams.surname" autocomplete="off" />
            </el-form-item>
            <el-form-item label="email" :label-width="formLabelWidth">
                <el-input v-model="formParams.email" autocomplete="off" />
            </el-form-item>
            <el-form-item label="phoneNumber" :label-width="formLabelWidth">
                <el-input v-model="formParams.phoneNumber" autocomplete="off" />
            </el-form-item>
            <el-form-item label="isActive" :label-width="formLabelWidth">
                <el-switch v-model="formParams.isActive" />
            </el-form-item>
            <el-form-item label="lockoutEnabled" :label-width="formLabelWidth">
                <el-switch v-model="formParams.lockoutEnabled" />
            </el-form-item>
            <el-form-item label="roleNames" :label-width="formLabelWidth">
                <el-select
                    v-model="formParams.roleNames"
                    multiple
                    collapse-tags
                    placeholder="Select"
                >
                    <el-option
                        v-for="item in roles"
                        :key="item.name"
                        :label="item.name"
                        :value="item.name"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="password" :label-width="formLabelWidth">
                <el-input v-model="formParams.password" autocomplete="off" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="emit('update:dialogVisible',false)">Cancel</el-button>
                <el-button type="primary" @click="sureClick">
                    Confirm
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup>
import { reactive, ref, watch } from 'vue'
import {usersCreate} from "@/api/user"
import { ElMessage, ElMessageBox } from 'element-plus'
import {omit,pick} from "lodash-es"
defineOptions({
  name: "UserDialog2",
});
const props = defineProps({
  dialogVisible: {
    type:Boolean,
    default:false
  },
  roles:{
    type:Array,
    default:function() {
      return []
    }
  },
  routeDatas:{
    type:Array,
    default:function() {
      return []
    }
  },
  type:{
    type:String,
    default:'add'
  },
  editObject:{
    type:Object,
    default:function() {
      return null
    }
  }
})
const emit = defineEmits(['update:dialogVisible','hanldeRequest']);
const formLabelWidth = '140px'
const formParams = ref({
  userName:'',
  name:'',
  surname:'',
  email:'',
  phoneNumber:'',
  isActive:true,
  lockoutEnabled:true,
  roleNames:[],
  password:'',
})

function handleOpen() {
  if(props.type === 'add') {
    formParams.value = {
      userName:'',
      name:'',
      surname:'',
      email:'',
      phoneNumber:'',
      isActive:true,
      lockoutEnabled:true,
      roleNames:[],
      password:'',
    }
  }else if(props.type === 'edit') {
    formParams.value = {...pick(props.editObject,['userName','name','surname','email','phoneNumber','isActive','lockoutEnabled']),roleNames:[],password:''} //Object.assign(formParams.value,props.editObject)
  }

}
async function sureClick() {
  if(props.type === 'add') {
    await usersCreate(formParams.value)
    ElMessage({
      type: 'success',
      message: '新增成功',
    })
  }else if(props.type === 'edit') {
  }

  emit('update:dialogVisible',false)
  emit('hanldeRequest')
}


</script>

<style lang="scss" scoped>

</style>